<?php
/*************************************
 *
 * @Controller 控制层主要接收与传递参数
 *
 ************************************/
namespace app\api\controller;

use app\common\services\ProductServices;
use app\common\services\UserServices;
use think\Env;

use think\Request;

use think\Validate;

use think\Response;

use think\config;
use \think\exception\HttpException;

class Product extends Base
{
    protected $productServices;

    protected $UserServices;

    protected $timeout = 10;

    protected $Request;

    public function __construct
    (
        Request $request
    )
    {
        parent::__construct($request);
        $this->productServices = new ProductServices();
        $this->UserServices = new UserServices();
    }

    /*
     *  产品列表
     *  @param Int type：旅游分类： 1自由行 2海口出行 3三亚出行
     */
    public function getProductList(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = $this->productServices->getFligtList($respons);

        if ($result) {
            return json(['data'=>$result, 'status_code'=>200]);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);
    }

    /*
     *  产品详情
     *  @param String session_key 登陆返回的字符串
     *  @param Int p_id 旅游产品id
     *  @param Int children_num 携带儿童数量
     *  @param Int adult_num 携带成人数量
     *  @param Int trips_time 启程时间
     */
    public function getProductInfo(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = $this->productServices->getFligtInfo($respons);
        if ($result) {
            return json(['data'=>$result, 'status_code'=>200]);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);

    }

    /*
     *  添加产品订单
     *  @param String session_key
     */
    public function getAddProduct(Request $request)
    {
        if(!Request::instance()->isPost()) {
            return json(['msg' => '非法请求！', 'status_code'=>400]);
        }
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $sessionKey = $respons['session_key'];
        $checkLogin = $this->UserServices->checkLogin($sessionKey);
        if (!$checkLogin) {
            return json(['msg' => '暂未登陆', 'status_code'=>400]);
        }

        if (empty($checkLogin['mobile'])) {
            return json(['msg' => '暂未绑定手机号码', 'status_code'=>401]);
        }

        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = true;
        if(isset($respons['p_id'])){
            if(!empty($respons['p_id'])){
                $respons['user_id'] = $checkLogin['id'];
                $result = $this->productServices->addProduct($respons);
            }
        }

        if ($result) {
            return json(['data'=>'预约成功!', 'status_code'=>200]);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);

    }

    /*
     *  订单查询
     */
    public function myOrder(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $sessionKey = $respons['session_key'];
        $checkLogin = $this->UserServices->checkLogin($sessionKey);
        if (!$checkLogin) {
            return json(['msg' => '暂未登陆', 'status_code'=>400]);
        }
        $respons['user_id'] = $checkLogin['id'];
        $msg = $this->productServices->selectOrder($respons);
        if ($msg) {
            return json(['data'=>$msg, 'status_code'=>200]);
        }
        return json(['data'=>[], 'status_code'=>201]);
    }

    /*
    *  绑定手机号码
    */
    public function bindMobile(Request $request)
    {
        if(!Request::instance()->isPost()) {
            return json(['msg' => '非法请求！', 'status_code'=>400]);
        }
        $respons = $request->param();
        $validate = new Validate([
            'mobile'  => 'require',
            'code' => 'require',
        ]);
        if (!$validate->check($respons)) {
            return json(['data'=>$validate->getError(), 'status_code' => 400]);
        }
        $sessionKey = $respons['session_key'];
        $checkLogin = $this->UserServices->checkLogin($sessionKey);
        if (!$checkLogin) {
            return json(['msg' => '暂未登陆', 'status_code'=>400]);
        }
        //用户id
        $respons['id'] = $checkLogin['id'];
        $msg = $this->productServices->bindCode($respons);
        if (is_array($msg) && isset($msg['error'])) {
            return json(['msg' => '验证码错误！', 'status_code'=>2001]);
        }
        if (!$msg) {
            return json(['msg' => '更新数据失败', 'status_code'=>2002]);
        }
        return json(['msg' => '更新成功', 'status_code'=>252], 200);
    }
}